package com.project.slsx.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.GoodslossListAdapter;
import com.project.slsx.bean.GoodsLossBean;
import com.project.slsx.bean.GoodsLossListBean;
import com.project.slsx.utils.ParamsUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

public class GoodsLossListActivity extends BaseActivity {

    private static final String TAG = GoodsLossListActivity.class.getSimpleName();
    private RecyclerView rv;
    private SmartRefreshLayout sfl;
    private int page = 1;
    private GoodslossListAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_goods_loss_list;
    }

    @Override
    public void initView() {
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());
        TextView title = findViewById(R.id.tv_title);
        title.setText("损耗列表");
        sfl = findViewById(R.id.smartrefreshlayout_goods_loss);
        rv = findViewById(R.id.recyclerview_goods_loss_list);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new GoodslossListAdapter(R.layout.layout_item_goods_loss);
        rv.setAdapter(adapter);

    }

    @Override
    public void initData() {
        sfl.autoRefresh();
        sfl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refresh(refreshLayout, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refresh(refreshLayout, true);
            }
        });
    }

    private void refresh(RefreshLayout refreshLayout, boolean b) {
        refreshLayout.getLayout().postDelayed(() -> {
            if (b) {
                page = 1;
            } else {
                ++page;
            }

            getGoodsLossList(b);

        }, 50);
    }


    private void getGoodsLossList(boolean b) {
        HttpParams lossslistParams = ParamsUtils.getLossslistParams();
        lossslistParams.put("page", page);
        lossslistParams.put("starttime", "2019-3-1");
        lossslistParams.put("endtime", "2019-3-12");
        OkGo.<String>post(Constans.BASEURL).tag("lossslist").params(lossslistParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: 0" + response.body());
                GoodsLossListBean goodsLossListBean = JSONObject.parseObject(response.body(), GoodsLossListBean.class);
                if (goodsLossListBean.getErrno() == Constans.SUCCESS) {
                    if (goodsLossListBean.getResult().size() > 0) {
                        if (b) {
                            adapter.setNewData(goodsLossListBean.getResult());
                        } else {
                            adapter.addData(goodsLossListBean.getResult());
                        }
                    } else {
                        showToast(mContext, "没有更多数据");
                    }
                } else {
                    showToast(mContext, "网络错误");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (b) {
                    sfl.finishRefresh();
                } else {
                    sfl.finishLoadMore();
                }
            }

        });
    }
}
